﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Views;
using CPPEI.Coolzon.Event;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using CPPEI.Coolzon.Event.UI;


namespace CPPEI.Coolzon.DisplayApp
{
    public partial class MainWindow
    {
        /// <summary>
        /// 收到网络状态的反馈事件信息
        /// </summary>
        /// <param name="event"></param>
        [Common.Subscribe]
        public void OnReceiveNetStateFeedbackEvent(Coolzon.Event.UI.UINetConnectionStateFeedbackEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                _isClientProxyConnected = @event.IsConnected();
                clientProxyState = @event.States;
                if (_settingWindow != null)
                {
                    _settingWindow.RefreshNetState(_isClientProxyConnected, clientProxyState);
                }
            });
        }

        [Subscribe]
        public void OnReceiveResponseSwitchSolutionEvent(CPPEI.Coolzon.Event.UI.UISwitchSolutionEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UI.UIContext.Current.RenderView;

                // 注册到网络的事件总线上
                Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(this);
                if (renderView != null)
                {
                    Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(renderView);
                }
                ReconnectThread.Stop();

                if (renderView != null)
                {
                    // 让当前场景destory
                    renderView.ClearAllParts();
                }

                CPPEI.Coolzon.UI.Views.LoadingView loadingView = new UI.Views.LoadingView(@event.SolutionName,
                    CZEnvironment.GetSolutionFilePath(@event.SolutionName),
                    CZEnvironment.GetSolutionDirectoryPath(@event.SolutionName));
                loadingView.Close += loadingView_Close;
                loadingView.LoadSolutionErrorEvent += LoadingView_LoadSolutionErrorEvent;
                _ctlViewContainer.SwitchView(loadingView);
                loadingView.Active();
            });
        }

        /// <summary>
        /// 收到全屏状态改变事件
        /// </summary>
        /// <param name="event"></param>
        [Subscribe]
        public void OnReceiveUIFullscreenStateChangedEvent(Event.UI.UIChangeFullScreenStateEvent @event)
        {
            if (UIContext.Current.IsFullScreen)
            {
                _xwindowHelper.ExitFullscreen();
                UIContext.Current.IsFullScreen = false;
                if (_settingWindow != null)
                {
                    _settingWindow.Activate();
                }
            }
            else
            {
                _xwindowHelper.EnterFullscreen();
                UIContext.Current.IsFullScreen = true;

                if (_settingWindow != null)
                {
                    _settingWindow.Activate();
                }
            }
        }

        [Subscribe]
        /// <summary>
        /// 自动播放
        /// </summary>
        public void OnAutoPlay(UIAutoPlayEvent @event)
        {
            if (@event.IsEnable)
            {
                if (dispatcherTime == null)
                {
                    dispatcherTime = new DispatcherTimer();
                    double interval = 3;
                    if (UIContext.Current.Solution.Displayflow.Interval != 0.0)
                    {
                        interval = UIContext.Current.Solution.Displayflow.Interval;
                    }
                    dispatcherTime.Interval = TimeSpan.FromSeconds(interval);
                    dispatcherTime.Tick += dispatcherTime_Tick;
                }
                dispatcherTime.IsEnabled = true;
                UIContext.Current.IsAutoPlay = true;
            }
            else
            {
                if (dispatcherTime != null)
                {
                    dispatcherTime.IsEnabled = false;
                }
                UIContext.Current.IsAutoPlay = false;
            }
        }
    }
}
